<?php
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
	
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/admin/model/question.php';
	
	class QuestionItemDao {
		private $_db;
	
		public function __construct() {
			$this->_db = new DbConnection();
		}
		
		public function deleteQuestionItem($catID) {
			$query = sprintf("DELETE from questions_item WHERE CategoryId = '%d'", $catID);
			$result = $this->_db->executeQuery($query);
			if (!$result) {
				echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
			    return 0;
			} 
			return mysql_affected_rows();
		}
		
		public function addQuestionItem($questionItem) {
			$query = sprintf("CALL sp_AddQuestionItem('%s', '%d', '%s', @QuestionId)", 
						$questionItem->getQuestion(),
						$questionItem->getCategoryID(),
						$questionItem->getAnswers());
			$result = $this->_db->executeQuery($query);
		
			if (!$result) {
			    echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
			    return 0;
			}
			
			$result = $this->_db->executeQuery('SELECT @QuestionId');
	
			if (!$result) {
			    echo 'Could not run query: ' . mysql_error();
			    return 0;
			}
			
			if ($row = mysql_fetch_assoc($result)) {
				$id = $row['@QuestionId'];
			}		
							
			mysql_free_result($result);
			
			return $id;						
		}
	}
?>